package handlers

import (
	"fmt"
	"net/http"
	"werewolf/core"
)

func ViewHandler(w http.ResponseWriter, r *http.Request) {       //  玩家查看自己的角色身份
	if r.Method != http.MethodGet { 
		http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
		return
	}

	room := r.URL.Query().Get("room")
	//role := r.URL.Query().Get("role")
	id := r.URL.Query().Get("board")

	handle, err := core.Connection()
	if err != nil {
		http.Error(w, "Connection failed: "+err.Error(), http.StatusInternalServerError)
		return
	}
	defer handle.Close()

	var ms string
	query := "SELECT Role FROM Player WHERE RmNo=? AND No=?"
	err = handle.QueryRow(query, room, id).Scan(&ms)
	if err != nil {
		http.Error(w, "Error fetching role: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Return the role found
	fmt.Fprintln(w, ms)
}
